package com.crm.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.crm.dao.SysDepartmentMapper;
import com.crm.model.SysDepartment;
import com.crm.model.entity.Tree;
import com.crm.service.SysDepartmentService;
import com.crm.util.UUIDUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

@Service
public class SysDepartmentServiceImpl extends BaseServiceImpl<SysDepartment> implements SysDepartmentService{

	@Autowired
	private SysDepartmentMapper sysDepartmentMapper; 
	
	@Override
	@Transactional
	public int insert(SysDepartment record) {
		
		String uuid = UUIDUtil.getUUID();
		record.setDepartmentId(uuid);
		
/*		//设置叶子节点
		record.setIsLeaf(1);
		
		//判断父节点是否为叶子节点，如果是叶子节点就改变状态
		String pid = record.getParentId();
		if(!ValidateUtil.isEmpty(pid) && !pid.equals("0")){
			SysDepartment pRecord = sysDepartmentMapper.selectByPrimaryKey(pid);
			if(pRecord.getIsLeaf() == 1){
				pRecord.setIsLeaf(0);
				sysDepartmentMapper.updateByPrimaryKeySelective(record);
			}
		}*/
		
//		if(ValidateUtil.isEmpty(record.getParentId())){
//			Integer maxSortCode = sysDepartmentMapper.selectMaxSortCode(new SysDepartment());
//			if(maxSortCode == null){
//				maxSortCode = 0;
//			}
//			record.setSortCode((maxSortCode/100000+1)*100000);
//			record.setParentId("0");
//		}else{
//			SysDepartment parentDepartment = sysDepartmentMapper.selectByPrimaryKey(record.getParentId());
//			int pSortCode = parentDepartment.getSortCode();
//			int departmentTmpl = 1;
//			while(true){
//				double departmentTest =Math.pow(5, departmentTmpl);
//				if(pSortCode % departmentTest != 0)
//					break;
//				departmentTmpl++;
//			}
//			SysDepartment t = new SysDepartment();
//			t.setParentId(parentDepartment.getDepartmentId());
//			Integer maxSortCode = sysDepartmentMapper.selectMaxSortCode(t);
//			if(maxSortCode == null)
//				maxSortCode = pSortCode;
//			record.setSortCode(parentDepartment.getSortCode() + new Double(Math.pow(10, departmentTmpl-2)).intValue());
//		}
		return super.insert(record);
	}
	
	

	@Override
	@Transactional
	public int updateByPrimaryKey(SysDepartment record) {
		
/*		//如果删除记录，改变其父节点
		if(record.getDeleteMark() == 1){
			//其父节点下是否有其它子节点
			List<SysDepartment> siblings = sysDepartmentMapper.selectSiblings();
			if(siblings.size() == 1){
				SysDepartment pRecord = sysDepartmentMapper.selectByPrimaryKey(siblings.get(0).getParentId());
				pRecord.setIsLeaf(1);
				sysDepartmentMapper.updateByPrimaryKeySelective(pRecord);
			}
		}*/
		
		return super.updateByPrimaryKey(record);
	}



	/**
	 * 树形结构数据查询
	 */
	@Override
	public List<Tree> selectMaster() {
		// TODO Auto-generated method stub
		return sysDepartmentMapper.selectMaster();
	} 
	
	@Override
	public List<SysDepartment> selectDeparmentAlle(Page<SysDepartment> page, SysDepartment t) {
		PageHelper.startPage(page.getPageNum(), page.getPageSize(), "code asc");
		t.setType(0);
		List<SysDepartment> list = sysDepartmentMapper.selectDeparmentAlle(t);
		return this.test(page, list);
	}

	//递归跑起来
	public List<SysDepartment> test(Page<SysDepartment> page, List<SysDepartment> list){
		for (SysDepartment sysDepartment : list){
			SysDepartment t = new SysDepartment();
			t.setParentId(sysDepartment.getDepartmentId());
			PageHelper.startPage(page.getPageNum(), page.getPageSize(), "code asc");
			List<SysDepartment> childsList = sysDepartmentMapper.selectDeparmentAlle(t);
			sysDepartment.setChilds(childsList);
			if(!sysDepartmentMapper.selectDeparmentAlle(t).isEmpty()){
				test(page, childsList);
			}
		}
		return list;
	}

	@Override
	public List<SysDepartment> selectAll(Page<SysDepartment> page, SysDepartment t) {
		
		PageHelper.startPage(page.getPageNum(), page.getPageSize(), "code asc");
		
		List<SysDepartment> list = sysDepartmentMapper.selectAllByWhere(t);
		
		return list;
	}
	
	
	@Override
	public List<SysDepartment> selectAll(Page<SysDepartment> page) {
		
		PageHelper.startPage(page.getPageNum(), page.getPageSize(), "code asc");
		
		List<SysDepartment> list = sysDepartmentMapper.selectAll();
		
		return list;
	}

	@Override
	public List<Tree> selectMasterByParm(Tree tree) {
		// TODO Auto-generated method stub
		return sysDepartmentMapper.selectMasterByParm(tree);
	}

	@Override
	public List<Tree> selectByParm(Tree tree) {
		return sysDepartmentMapper.selectByParm(tree);
	}
	
	
}
